package fr.upmc.mdoc.tp1.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import fr.upmc.mdoc.tp1.domain.DAOContact;
import fr.upmc.mdoc.tp1.domain.DAOContactGroup;
import fr.upmc.mdoc.tp1.domain.DAOPhoneNumber;
import fr.upmc.mdoc.tp1.models.Contact;
import fr.upmc.mdoc.tp1.models.ContactGroup;
import fr.upmc.mdoc.tp1.models.PhoneNumber;

public class EditContactAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		DAOContact dao = new DAOContact();
		DAOContactGroup daog = new DAOContactGroup();
		DAOPhoneNumber daop = new DAOPhoneNumber();

		// Récupération de l'id à partir de l'URL
		int idContact = Integer.valueOf(request.getAttribute("id").toString());

		Contact contact = dao.getContact(idContact);
		List<PhoneNumber> phones = daop.getPhones(idContact);
		List<ContactGroup> groups = daog.getGroups(idContact);

		request.setAttribute("contactId", contact.getContactId());
		request.setAttribute("fn", contact.getFirstName());
		request.setAttribute("ln", contact.getLastName());
		request.setAttribute("email", contact.getEmail());
		request.setAttribute("street", contact.getAddress().getStreet());
		request.setAttribute("city", contact.getAddress().getCity());
		request.setAttribute("zip", contact.getAddress().getZip());
		request.setAttribute("country", contact.getAddress().getCountry());

		dao.editContat(idContact);

		mapping.findForward("pages/editContact.jsp");

		return super.execute(mapping, form, request, response);
	}
}
